home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / text / edit / tecoc-146.lha / init.c < prev    next >
C/C++ Source or Header  |  1991-07-05  |  5KB  |  200 lines

  1. /*****************************************************************************
  2.     Init.c
  3.  
  4.     Initialization routines for Tecoc.
  5. *****************************************************************************/
  6.  
  7. #include "zport.h"        /* define portability identifiers */
  8. #include "tecoc.h"        /* define general identifiers */
  9. #include "defext.h"        /* define external global variables */
  10. #include "deferr.h"        /* define identifiers for error messages */
  11.  
  12. #if USE_PROTOTYPES
  13. static VVOID MemIni(void);
  14. #endif
  15.  
  16. /*****************************************************************************
  17.  
  18.     MemIni()
  19.  
  20.     This function allocates memory for the edit buffer,  command buffer,
  21. filename buffer and search string buffer.  System-independent global
  22. variables are also initialized by this function.
  23.  
  24. *****************************************************************************/
  25.  
  26. static VVOID MemIni()
  27. {
  28.     WORD    i;
  29.     QRptr    QRp;
  30.     charptr TmpMem;
  31.  
  32.     DBGFEN(2,"MemIni",NULL);
  33.  
  34. /*
  35.  * allocate a chunk of memory to start with which is free'd as soon as
  36.  * MemIni exits.  subsequent small memory allocations might be taken from
  37.  * this free'd chunk without fragmenting the end of the heap where the
  38.  * edit buffer resides.
  39.  */
  40.  
  41.     TmpMem = (charptr)ZAlloc((SIZE_T)(4*1024));
  42.  
  43. /*
  44.  * allocate memory for the digit buffer.  It needs to be big enough to hold
  45.  * the decimal (signed), octal or hexadecimal representation of the largest
  46.  * number that can be stored in a LONG.  Here, it's assumed that octal takes
  47.  * the most characters.  The calculation is as follows:
  48.  *
  49.  *    sizeof(LONG) * 8 to get bits per long
  50.  *    +2 to round up so /3 doesn't truncate
  51.  *    /3 since there is 3 bits per octal digit
  52.  *    +2 in case ExeEqu() appends a <CR><LF> for display purposes
  53.  */
  54.  
  55.     DBfBeg = DBfPtr = (charptr)ZAlloc((((sizeof(LONG)*8)+2)/3)+2);
  56.     if (DBfBeg == NULL) {
  57.         ErrMsg(ERR_MEM);        /* MEM = memory overflow */
  58. #if DEBUGGING
  59.         sprintf(DbgSBf,"DBfBeg = ZAlloc(15) failed, calling TAbort()");
  60.         DbgFMs(2,DbgFNm,DbgSBf);
  61. #endif
  62.         TAbort(EXIT_FAILURE);
  63.     }
  64.  
  65. /*
  66.  * allocate memory for the filename buffer
  67.  */
  68.  
  69.     FBfBeg = FBfPtr = (charptr)ZAlloc((SIZE_T)FILENAME_MAX);
  70.     if (FBfBeg == NULL) {
  71.         ErrMsg(ERR_MEM);        /* MEM = memory overflow */
  72. #if DEBUGGING
  73.         sprintf(DbgSBf,"FBfBeg = ZAlloc(%ld) failed, calling TAbort()",
  74.             (LONG)FILENAME_MAX);
  75.         DbgFMs(2,DbgFNm,DbgSBf);
  76. #endif
  77.         TAbort(EXIT_FAILURE);
  78.     }
  79.     FBfEnd = FBfBeg + FILENAME_MAX - 1;
  80.  
  81. /*
  82.  * allocate memory for the command buffer
  83.  */
  84.  
  85.     CBfBeg = (charptr)ZAlloc((SIZE_T)CBFINIT);
  86.     if (CBfBeg == NULL) {
  87.         ErrMsg(ERR_MEM);        /* MEM = memory overflow */
  88. #if DEBUGGING
  89.         sprintf(DbgSBf,"CbfBeg =ZAlloc(%ld) failed, calling TAbort()",
  90.             (LONG)CBFINIT);
  91.         DbgFMs(2,DbgFNm,DbgSBf);
  92. #endif
  93.         TAbort(EXIT_FAILURE);
  94.     }
  95.     CBfEnd = CBfBeg + CBFINIT - 1;
  96.  
  97. /*
  98.  * allocate memory for the search string buffer
  99.  */
  100.  
  101.     SBfBeg = SBfPtr = (charptr)ZAlloc((SIZE_T)SBFINIT);
  102.     if (SBfBeg == NULL) {
  103.         ErrMsg(ERR_MEM);        /* MEM = memory overflow */
  104. #if DEBUGGING
  105.         sprintf(DbgSBf,"SbfBeg = ZAlloc(%ld) failed, calling TAbort()",
  106.             (LONG)SBFINIT);
  107.         DbgFMs(2,DbgFNm,DbgSBf);
  108. #endif
  109.         TAbort(EXIT_FAILURE);
  110.     }
  111.     SBfEnd = SBfBeg + SBFINIT - 1;
  112.  
  113. /*
  114.  * allocate memory for the edit buffer and input buffer
  115.  */
  116.  
  117.     EBfBeg = GapBeg = (charptr)ZAlloc((SIZE_T)EBFINIT + (SIZE_T)IBFINIT);
  118.     if (EBfBeg == NULL) {
  119.         ErrMsg(ERR_MEM);        /* MEM = memory overflow */
  120. #if DEBUGGING
  121.         sprintf(DbgSBf,"EbfBeg = ZAlloc(%ld) failed, calling TAbort()",
  122.             (LONG)(EBFINIT + IBFINIT));
  123.         DbgFMs(2,DbgFNm,DbgSBf);
  124. #endif
  125.         TAbort(EXIT_FAILURE);
  126.     }
  127.     EBfEnd = GapEnd = EBfBeg + EBFINIT - 1;
  128.     IBfEnd = EBfEnd + IBFINIT;
  129.  
  130. /*
  131.  * clear the global [0-35] and main-level local [36-71] Q-registers
  132.  */
  133.  
  134.     for (QRp = QRgstr, i = 0; i < 72; ++i, ++QRp) {
  135.         QRp->Start = QRp->End_P1 = NULL;
  136.         QRp->Number = 0;
  137.     }
  138.  
  139. /*
  140.  * Clear the Q-register stack
  141.  */
  142.  
  143.     for (QRp = QStack, i = 0; i < QRS_SIZE; ++i, ++QRp) {
  144.         QRp->Start = QRp->End_P1 = NULL;
  145.         QRp->Number = 0;
  146.     }
  147.  
  148. /*
  149.  * Initialize the file-open and end-of-file indicators.
  150.  */
  151.  
  152.     for (i = 0; i < NIFDBS; i++) {
  153.         IsOpnI[i] = IsEofI[i] = FALSE;
  154.     }
  155.     for (i = 0; i < NOFDBS; i++) {
  156.         IsOpnO[i] = FALSE;
  157.     }
  158.  
  159. /*
  160.  * Free our small allocation memory pool.
  161.  */
  162.  
  163.     if (TmpMem) {
  164.         ZFree (TmpMem);
  165.     }
  166.  
  167.     DBGFEX(2,DbgFNm,NULL);
  168. }
  169.  
  170.  
  171. /*****************************************************************************
  172.  
  173.     Init()
  174.  
  175.     This function initializes TECO-C.
  176.  
  177.     If an error is detected in any of the functions called by this
  178. function,  a message is displayed and the program terminates.
  179.  
  180. *****************************************************************************/
  181.  
  182. VVOID Init(argc, argv)        /* initialize TECO-C */
  183. int argc;
  184. char **argv;
  185. {
  186.     DBGFEN(2,"Init",NULL);
  187.  
  188.     ZTrmnl();        /* open terminal for input and output */
  189.     MemIni();        /* allocate memory and initialize variables */
  190.  
  191. #if CONSISTENCY_CHECKING
  192.     init_consistency_check();
  193.     check_consistency();
  194. #endif
  195.  
  196.     ZPrsCL(argc, argv);    /* parse the command line */
  197.  
  198.     DBGFEX(2,DbgFNm,NULL);
  199. }
  200.